home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / stdlib / system.c < prev   
C/C++ Source or Header  |  1995-12-23  |  3KB  |  107 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #define KERNEL
  21. #include "ixemul.h"
  22. #include "kprintf.h"
  23. #include <sys/wait.h>
  24. #include <ctype.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. /* 2.0 support */
  29. #include <utility/tagitem.h>
  30. #include <dos/dostags.h>
  31.  
  32. #define STACKNAME ("IXSTACK")    /* Environment variable for stack size */
  33. #define STACKSIZE (16384)    /* Minimum stack size to use */
  34.  
  35. int get_stack_size(struct Process *proc)
  36. {
  37.   struct CommandLineInterface *CLI;
  38.   int stack_size;
  39.   char *tmp;
  40.  
  41.   CLI = BTOCPTR (proc->pr_CLI);
  42.   if ((tmp = getenv (STACKNAME)))
  43.     stack_size = atoi (tmp);
  44.   else
  45.     stack_size = CLI ? CLI->cli_DefaultStack * 4 : proc->pr_StackSize;
  46.   if (stack_size < STACKSIZE)
  47.     return STACKSIZE;
  48.   return stack_size;
  49. }
  50.  
  51. int system (const char *cmd)
  52. {
  53.   int rc = -1, err = 0;
  54.   int stack_size;
  55.   struct Process *me;
  56.   int omask;
  57.  
  58.   if (cmd == NULL)
  59.     return 1;
  60.   /* I retry with our new signal mechanism ;-) */
  61.   omask = syscall (SYS_sigsetmask, ~0);
  62.  
  63.   me = (struct Process *)FindTask(0);
  64.   stack_size = get_stack_size(me);
  65.  
  66.   /* limited support to allow starting of files in the current directory
  67.    * with `./foo'. The better approach would use the __plock() trick to
  68.    * parse the command, LoadSeg it. But then this approach would have to
  69.    * do the whole redirection stuff on its own.. */
  70.   while (isspace (*cmd)) cmd++;
  71.   while (cmd[0] == '.' && cmd[1] == '/') cmd += 2;
  72.   /* convert absolute path names if enabled in ixemulbase */
  73.   if ((ix.ix_flags & ix_translate_slash) && cmd[0] == '/')
  74.     {
  75.       char *path_sep = NULL;
  76.  
  77.       cmd++;
  78.       if ((path_sep = strchr (cmd, '/')))  *path_sep = ':';
  79.     }
  80.  
  81.     {
  82.       struct TagItem tags [] =
  83.         {
  84.       /* a stack of 4K is generally ways too small.. */
  85.       { NP_StackSize, stack_size, },
  86.       { TAG_END, 0, }
  87.         };
  88.  
  89.       rc = SystemTagList ((UBYTE *)cmd, tags);
  90.       err = __ioerr_to_errno (IoErr ());
  91.  
  92.       syscall (SYS_sigsetmask, omask);
  93.  
  94.       if (rc > 128)
  95.         errno = EINTR;
  96.       else 
  97.         errno = err;
  98.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  99.  
  100.       /* according to the BSD manual, system() should return the `exit status'
  101.        * of the shell, the implementation returns the wait-status. So I return
  102.        * an artificial wait status for now ...
  103.        */
  104.       return (rc >= 128) ? W_EXITCODE (0, rc & 0x7f) : W_EXITCODE (rc, 0);
  105.     }
  106. }
  107.